package order

import (
	"context"
	"gitee.com/fangy1111/shop/order"
	"github.com/gin-gonic/gin"
	"net/http"
	"order_bff/global"
	"strconv"
)

// 创建订单
func Create(c *gin.Context) {
	userId, _ := c.Get("uid")
	address := c.PostForm("address")
	name := c.PostForm("name")
	mobile := c.PostForm("mobile")
	post := c.PostForm("post")
	create, err := global.OrderSrvConn.Create(context.Background(), &order.OrderRequest{
		UserId:  userId.(int32),
		Address: address,
		Name:    name,
		Mobile:  mobile,
		Post:    post,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "创建订单失败",
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":     0,
		"msg":      "success",
		"order_id": create.Id,
		"data1": map[string]interface{}{
			"orderData": create,
		},
	})
}

// 订单列表
func List(c *gin.Context) {
	userId, _ := c.Get("uid")

	page := c.Query("page")
	pageInt, _ := strconv.Atoi(page)

	pageSize := c.Query("pagesize")
	pageSizeInt, _ := strconv.Atoi(pageSize)

	payType := c.Query("paytype")
	payTypeInt, _ := strconv.Atoi(payType)

	status := c.Query("status")
	statusInt, _ := strconv.Atoi(status)

	list, err := global.OrderSrvConn.OrderList(context.Background(), &order.OrderFilterRequest{
		UserId:      userId.(int32),
		Pages:       int32(pageInt),
		PagePerNums: int32(pageSizeInt),
		PayType:     order.OrderFilterRequest_PayTypes(payTypeInt),
		Status:      int32(statusInt),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "查询订单列表失败",
			"data": nil,
		})
		return
	}
	reMap := gin.H{
		"total": list.Total,
	}

	orderList := make([]interface{}, 0)

	for _, v := range list.Data {
		tmpMap := map[string]interface{}{}
		tmpMap["id"] = v.Id
		tmpMap["status"] = v.Status
		tmpMap["pay_type"] = v.PayType
		tmpMap["user_id"] = v.UserId
		tmpMap["post"] = v.Post
		tmpMap["order_mount"] = v.Total
		tmpMap["address"] = v.Address
		tmpMap["signer_name"] = v.Name
		tmpMap["signer_mobile"] = v.Mobile
		tmpMap["order_sn"] = v.OrderSn
		orderList = append(orderList, tmpMap)
	}
	reMap["data"] = orderList
	c.JSON(http.StatusOK, reMap)
}

// 订单详情
func Detail(c *gin.Context) {
	orderSn := c.Query("order_sn")
	detail, err := global.OrderSrvConn.OrderDetail(context.Background(), &order.OrderRequest{
		OrderSn: orderSn,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "查询订单列表失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": detail,
	})
}

// 更新订单
func Update(c *gin.Context) {
	orderSn := c.PostForm("order_sn")
	payType := c.PostForm("pay_type")
	status := c.PostForm("status")
	tradeNo := c.PostForm("trade_no")
	_, err := global.OrderSrvConn.UpdateOrder(context.Background(), &order.UpdateOrderInfo{
		PayType: payType,
		Status:  status,
		TradeNo: tradeNo,
		OrderSn: orderSn,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "更新订单编号失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": nil,
	})
}
